#include "yxtabledelegate.h"
#include "headandcomb.h"

#include <QLineEdit>
#include <QComboBox>
YXTableDelegate::YXTableDelegate(QTableView* v,YXTableModel* m,QObject *parent) : \
    view(v),mode(m),QItemDelegate(parent)
{}

QWidget* YXTableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, \
                        const QModelIndex &index) const{
    if(index.column() == 0){
        QComboBox *editor =  new  QComboBox(parent);
        HeadAndComb head;
        editor->addItems(head.combitem[0]);
        return editor;
    }
    if(index.column() == 1){
        QComboBox *editor =  new  QComboBox(parent);
        HeadAndComb head;
        editor->addItems(head.combitem[1]);
        return editor;
    }
    if(index.column() == 2){
        int row = index.row(),col = index.column();
        QModelIndex temp = index.model()->index(row,--col);
        QString text = temp.data().toString();
        qDebug() << "text:" << text;
        HeadAndComb head;
        QComboBox *editor =  new  QComboBox(parent);
        if(text == "遥信值"){
            qDebug() << "yx";
            editor->addItems(head.combitem[2]);
        }
        if(text == "保护"){
            qDebug() << "bh";
            editor->addItems(head.combitem[3]);
        }
        connect(editor,SIGNAL(currentTextChanged(QString)),\
                this,SLOT(combox_slot(QString)));
        return editor;
    }
    QLineEdit *editor =  new  QLineEdit(parent);
    return  editor;
}
void YXTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    if(index.column() == 0 || index.column() == 1 || index.column() == 2){
        QString text = index.model()->data(index, Qt::EditRole).toString();
        QComboBox *comboBox =  static_cast <QComboBox*>(editor);
        int  num = comboBox->findText(text);
        comboBox->setCurrentIndex(num);
        return;
    }
    QString text = index.model()->data(index, Qt::EditRole).toString();
    QLineEdit *lineEdit =  static_cast <QLineEdit*>(editor);
    lineEdit->setText(text);
}
void YXTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, \
                        const QModelIndex &index) const{
    if(index.column() == 0 || index.column() == 1 || index.column() == 2){
        QComboBox *comboBox =  static_cast <QComboBox*>(editor);
        QString text = comboBox->currentText();
        model->setData(index, text, Qt::EditRole);
        return;
    }
    QLineEdit *lineEdit =  static_cast <QLineEdit*>(editor);
    QString text = lineEdit->text();
    model->setData(index, text, Qt::EditRole);
}
void YXTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,\
                        const QModelIndex &index) const{
    editor->setGeometry(option.rect);
}
void YXTableDelegate::combox_slot(QString data){
    QComboBox* tempComb = (QComboBox*)sender();//发送对象转换，可以精确到发送的具体对象
    int x = tempComb->frameGeometry().x(),y = tempComb->frameGeometry().y();//发送对象的窗口坐标
    QModelIndex index = view->indexAt(QPoint(x,y));
    if(!index.isValid()){
        qDebug() << "index unvalid";
        return;
    }
    int row = index.row(),col = index.column();
    //因为index.model()返回的model对象是const的，而setData函数不是const函数
    //所以这里不能用
    //QModelIndex next = index.model()->index(row,++col);
    QModelIndex next = mode->index(row,++col);
    if(!next.isValid()){
        qDebug() << "next index unvalid";
        return;
    }
    if(data == "合闸"){
        mode->setData(next,QString("合闸"),Qt::EditRole);
        return;
    }
    if(data == "分闸"){
        mode->setData(next,QString("分闸"),Qt::EditRole);
        return;
    }
    if(data == "储能"){
        mode->setData(next,QString("储能"),Qt::EditRole);
        return;
    }
    if(data == "未储能"){
        mode->setData(next,QString("未储能"),Qt::EditRole);
        return;
    }
}
